package com.dhcc.bpm.modules.scf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.utils.StringUtils;
import com.dhcc.bpm.modules.scf.dto.ScfTruckInfoDTO;
import com.dhcc.bpm.modules.scf.entity.ScfTruckInfo;
import com.dhcc.bpm.modules.scf.service.IScfTruckInfoService;
import com.dhcc.bpm.modules.scf.vo.ScfTruckInfoVO.ScfGetTruckDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfTruckInfoVO.ScfGetTruckListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@AllArgsConstructor
public class ScfTruckInfoController extends BladeController {

    private IScfTruckInfoService scfTruckInfoService;


    /**
     * 查询车辆信息分页列表
     *
     * @param scfTruckInfo
     * @param query
     * @return
     */
    @GetMapping("/scfGetTruckList")
    // @Cacheable(value = "scfTruckListCache", key = "'scfTruckListCacheKey-' + #scfTruckInfo.hashCode() + '-' + #query.hashCode()", unless = "#result == null")
    public R<IPage<ScfGetTruckListVO>> pageList(ScfTruckInfoDTO scfTruckInfo, Query query) {
        if (StringUtils.isEmpty( scfTruckInfo.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }
        scfTruckInfo.setOrgCodes(  scfTruckInfo.getOrgCode().split(",") );

        if(scfTruckInfo.getOrgCode().contains( "50000000")){
            scfTruckInfo.setOrgCode("50000000");
            scfTruckInfo.setOrgCodes("50000000".split(",") );
        }

        return R.data(scfTruckInfoService.selectScfTruckInfoPage(Condition.getPage(query), scfTruckInfo));
    }

    /**
     * 查询车辆详情信息
     *
     * @param scfTruckInfo
     * @return
     */
    @GetMapping("/scfGetTruckDetails")
    // @Cacheable(value = "scfTruckDetailsCache", key = "'scfTruckDetailsCacheKey-' + #scfTruckInfo.rowId")
    public R<ScfGetTruckDetailsVO> getDetails(ScfTruckInfoDTO scfTruckInfo) {
        return R.data(scfTruckInfoService.getDetails(scfTruckInfo));
    }

    /**
     * 新增车辆信息
     *
     * @param scfTruckInfo
     * @return
     */
    @PostMapping("scfAddTruckDetails")
    // @CacheEvict(value = "scfTruckListCache", allEntries = true)
    public R<Boolean> saveTruck(@RequestBody ScfTruckInfoDTO scfTruckInfo) throws Exception {
        return R.status(scfTruckInfoService.saveTruck(scfTruckInfo));
    }

    /**
     * 修改车辆信息
     *
     * @param scfTruckInfo
     * @return
     */
    @PostMapping("/scfModTruckDetails")
    // @CacheEvict(value = "scfTruckDetailsCache", key = "'scfTruckDetailsCacheKey-' + #scfTruckInfo.rowId")
    public R<Boolean> updateTruck(@RequestBody ScfTruckInfoDTO scfTruckInfo) {
        return R.status(scfTruckInfoService.updateTruck(scfTruckInfo));
    }

    /**
     * 批量导入车辆信息
     *
     * @param operatorId 上传用户的id
     * @param file       文件
     * @return
     * @throws IOException
     */
    @PostMapping("/scfTruckBatchFile")
    // @CacheEvict(value = "scfFileInfoListCache", allEntries = true)
    public R<Boolean> uploadFile(@RequestParam("operatorId") String operatorId, MultipartFile file) throws Exception {
        // 获取到批量导入的数据集合
        List<ScfTruckInfo> scfTruckInfos = scfTruckInfoService.batchSaveScfTruckInfo(operatorId, file);
        int bachSaveTruck = scfTruckInfoService.bachSaveTruck(scfTruckInfos, operatorId);
        int total = scfTruckInfos.size();
        int totalSussess = bachSaveTruck;
        int totalFail = total - totalSussess;
        String msg = "解析到" + total + "条数据，" + "导入成功" + totalSussess + "条，导入失败" + totalFail + "条！";
        return R.success(msg);
    }


    /**
     * 批量删除车辆信息
     * @param ids
     * @return
     */
    @PostMapping("/scfGetTruck/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {

        String[] split = ids.split(",");

        return R.status(scfTruckInfoService.deleteByIds( split ));
    }

}
